package com.why.blog.admin.post;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.why.blog.common.R;
import com.why.blog.common.ResponseCode;
import com.why.blog.entity.HaloPost;
import com.why.blog.entity.JwtUser;
import com.why.blog.entity.VO.PostVO;
import com.why.blog.service.PostService;
import org.apache.commons.lang3.StringUtils;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * @author TriggerWu
 * @title post
 * @description 发帖管理
 * @updateTime 2019/5/1 22:20
 * @throws
 */
@RestController
@RequestMapping("/post")
public class PostContrller {

    @Autowired
    private PostService postService;

    @GetMapping("/")
    public ModelAndView tt() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("blog/list");
        return mv;
    }

    @GetMapping("/writePage")
    public ModelAndView write() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("blog/blogAdd");
        return mv;
    }



    @PreAuthorize("hasRole('ADMIN')")//必须要管理员身份
    @PostMapping("/po")
    public R userpo(@RequestBody Map map) {
        Object vo = map.get("PostVo");
        JwtUser user = JSONObject.parseObject(map.get("user").toString(), JwtUser.class);
        PostVO po = beanMapper.map(vo, PostVO.class);
        return postService.savePo(po,user);
    }

    @PreAuthorize("hasRole('ADMIN')")//必须要管理员身份
    @DeleteMapping("/del/{poId}")
    public R removePo(@PathVariable("poId") String poId) {
        return postService.removePo(poId);
    }

    @GetMapping("/showList")
    public Object showPolist(@RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "limit", defaultValue = "30") Integer pageSize,
                             @RequestParam(value = "createTime",required = false) Long createTime,
                             @RequestParam(value = "title",required = false) String title) {
        PostVO postVO = new PostVO();
        postVO.setPostDate(createTime);
        postVO.setPostTitle(title);
        IPage<HaloPost> iPage = postService.showList(postVO, page, pageSize);
        JSONObject result = new JSONObject();
        if (iPage.getRecords().size() == 0) {
            result.put("code", "0");
            result.put("data", iPage.getRecords());
            result.put("msg", "没有更多数据");
            result.put("count", iPage.getTotal());
            return result;
        }
        result.put("code", "0");
        result.put("data", iPage.getRecords());
        result.put("msg", "");
        result.put("count", iPage.getTotal());
        return result;
    }

    @Autowired
    Mapper beanMapper;

    @PreAuthorize("hasRole('ADMIN')")//必须要管理员身份
    @PutMapping("/update")
    public R updatePost(@RequestBody Map map) {
        Object vo = map.get("PostVo");
        JwtUser user = JSONObject.parseObject(map.get("user").toString(), JwtUser.class);
        PostVO po = beanMapper.map(vo, PostVO.class);
        if (StringUtils.isNotBlank(user.getUserId())) {
            po.setUserId(user.getUserId());
        } else {
            return R.getInstance(ResponseCode.USER_NOT_LOGIN.getCode(), ResponseCode.USER_NOT_LOGIN.getMsg());
        }
        return postService.updatePo(po);
    }
}
